package com.ziq.zcamp.manager

import com.gmlive.camp.data.exception.LocalException
import com.ziq.base.extension.launchWithTryAsyncGlobal
import com.ziq.zcamp.api.request.life.MyCampingDataParam
import com.ziq.zcamp.api.service.NetWorkException
import com.ziq.zcamp.api.service.PostingService
import com.ziq.zcamp.api.service.request
import com.ziq.zcamp.bean.life.MyCampingDataInfo
import dagger.hilt.EntryPoint
import dagger.hilt.InstallIn
import dagger.hilt.android.EntryPointAccessors
import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.withContext

object CampLifeManager {

    //我的露营数据
    val myCampData = MutableSharedFlow<MyCampingDataInfo>(replay = 0)

    fun requestMyCampData() {
        launchWithTryAsyncGlobal(whenTry = {
            val entryPoint = EntryPointAccessors.fromApplication(
                GlobalContextManager.appContext!!,
                ServiceEntryPointManager::class.java
            )
            val respond = entryPoint.postingService().myCampingData(MyCampingDataParam().queryMap).request()
            respond?.let {
                myCampData.emit(it.data?: MyCampingDataInfo())
            }
        }, whenCatch = {
            withContext(Dispatchers.Main) {
                when (it) {
                    is NetWorkException -> {
                        it.printStackTrace()
                    }
                    is LocalException -> {
                        it.printStackTrace()
                    }
                }
            }
        })

    }
}